// Fill out your copyright notice in the Description page of Project Settings.


#include "SGameGameInstance.h"
#include "UObject/UObjectGlobals.h"
#include "MoviePlayer/Public/MoviePlayer.h"
#include "Blueprint/UserWidget.h"


void USGameGameInstance::Init()
{
	UGameInstance::Init();
#if !UE_SERVER
	FCoreUObjectDelegates::PreLoadMap.AddUObject(this, &USGameGameInstance::BeginLoadingScreen);
	FCoreUObjectDelegates::PostLoadMapWithWorld.AddUObject(this, &USGameGameInstance::EndLoadingScreen);
#endif
}

void USGameGameInstance::Shutdown()
{
	Super::Shutdown();
	FCoreUObjectDelegates::PreLoadMap.RemoveAll(this);
	FCoreUObjectDelegates::PostLoadMapWithWorld.RemoveAll(this);
}

void USGameGameInstance::BeginLoadingScreen(const FString& MapName)
{
	FLoadingScreenAttributes LoadingScreen;
	LoadingScreen.bAutoCompleteWhenLoadingCompletes=false;
	LoadingScreen.bWaitForManualStop=false;
	LoadingScreen.bMoviesAreSkippable=false;
	LoadingScreen.MinimumLoadingScreenDisplayTime=1.5f;
	LoadingScreen.WidgetLoadingScreen= FLoadingScreenAttributes::NewTestLoadingScreenWidget();
	if (LoadingUMG!=nullptr)
	{
		CurrentWidget=CreateWidget<UUserWidget>(this,LoadingUMG);
		TSharedPtr<SWidget> LoadScreen= CurrentWidget->TakeWidget();
		LoadingScreen.WidgetLoadingScreen=LoadScreen;
	}
	GetMoviePlayer()->SetupLoadingScreen(LoadingScreen);
}

void USGameGameInstance::EndLoadingScreen(UWorld*  LoadedWorld )
{
	GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::Printf(TEXT("地图加载完成")));
}
